from spe import registerPreHook, unregisterPreHook
from playerlib import getPlayer
from superdice.libs.diceConfigs import configFile
from superdice.superdice import registerRoll, unregisterRoll, players

def es_map_start(ev):
    cfgData.execute()

def unload():
    unregisterPreHook('player_hurt', player_hurt_pre)
    unregisterRoll(diceRoll)

def player_spawn(ev):
    players[ev['userid']]['tank'] = 0

def player_hurt_pre(ev):
    userid = str(ev['userid'])
    if not players[userid]['tank'] or getPlayer(userid).isdead or not str(ev['attacker']) in players:
        return

    players[userid]['tank'] -= 1
    getPlayer(userid).health += int(ev['dmg_health'])

def diceRoll(userid, player, eplayer):
    players[userid]['tank'] = int(count)

cfgData = configFile('dice_tank')
count = cfgData.cvar('superdice_cfg_tank_count', 2, 'How many hits can the player block?')
cfgData.write()
cfgData.execute()

registerPreHook('player_hurt', player_hurt_pre)

registerRoll(diceRoll, 'tank')
